import React from 'react';
import ReactNative, {
  View,
  Image,
  Text,
  ListView,
  TextInput,
  Button,
  TouchableOpacity,
  Keyboard,
  ScrollView,
  Alert,
} from 'react-native';

import BasePage from '../common/BasePage';
import SizeUtils from '../../common/SizeUtils';
import Config from '../../config';
import ToastUtils from '../../common/ToastUtils';
import dismissKeyboard from 'dismissKeyboard';
import CommonShowViewFullScreenAction from '../../actions/common/CommonShowViewFullScreenAction';
import ActivityRequestEnterAutoAction from '../../actions/activity/ActivityRequestEnterAutoAction';
import CommonDataEditListPage from '../common/CommonDataEditListPage';


export default class ActivityApplicantPage extends CommonDataEditListPage {
  constructor(props) {
    super(props);
    this.state = {
      };
    this.state.activityData = this.props.pageData.routeData,
    this.pageData = {};
    this.state.statusText = "";
    this.state.viewStatus = 1;
    this._submitText = "报名";
    this._isShowSubmit = true;

     this._sendRequest = false;
     this._initPageData();
  }


  _dealEnterPage() {
    super._dealEnterPage();
    if(this.state.activityData != this.props.pageData.routeData){
      this.state.activityData = this.props.pageData.routeData;
      this._initPageData();
    }
  }

  _initPageButtonValues() {
    this._pageButtonValues = {};
  }

  _getPageDatas() {
    return  this.pageData;
  }

  _initPageData(){
    this.pageData = {};
    if(this.state.activityData.apply_fields && this.state.activityData.apply_fields.length > 0){
      for (var i = 0; i < this.state.activityData.apply_fields.length; i++) {
        var field = this.state.activityData.apply_fields[i];
        this.pageData[field.name] = {
          "isNeed":field.require == 1,
          key:field.field_id,
          "type":field.type == 1?"text":(field.type == 2?"single":"multi"),
          name:field.name,
          "fields":field.fields||[],
          "params":{"length":[2]}};
        }
      }
  }

  componentWillUpdate(nextProps, nextState) {
      if(nextState.viewStatus > 2 && this._sendRequest) {
        new CommonShowViewFullScreenAction().call(this, null);

        this._sendRequest = false;
        if(nextState.viewStatus == 3) {
          Alert.alert('提示', nextState.detailText ,[
              {text:'确定',onPress:()=>{
                this._pageButtonValues = {};
                this.goBack()
              }}
            ])
        }
        else {
          Alert.alert('提示', nextState.detailText, [
              {text:'确定'}
            ])
        }

        nextState.viewStatus = 1;
      }
    }

  _onPressSubmit(){
    this._callCreateSuggest()
}

_callCreateSuggest() {
  if(this._sendRequest){
    return;
  }
    var apply_fields = {};
   for (var i = 0; i < this.state.activityData.apply_fields.length; i++) {
        var field = this.state.activityData.apply_fields[i];
        if(field.require && this._pageButtonValues[field.field_id] === undefined){
          ToastUtils.show("请输入"+field.name);
          return;
        }

        if(this._pageButtonValues[field.field_id] !== undefined && this._pageButtonValues[field.field_id]!=""){
          apply_fields[field.field_id] = this._pageButtonValues[field.field_id];
        }
    }
  new ActivityRequestEnterAutoAction().call(this, {
    avatar_url:this.state.activityData.userData.avatar_url,
    activity_id:this.state.activityData.activity_id,
    apply_fields:apply_fields
   });
   this._sendRequest = true;
 }

}
